package koction.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import koction.model.product.ProductVO;
import koction.model.seller.FileVO;
import koction.model.seller.SellerService;
import koction.model.seller.SellerVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SellerController {
	@Resource(name="sellerServiceImpl")
	private SellerService sellerService;
	@RequestMapping("/getProductList.do")
	public ModelAndView getProductList(HttpSession session){
		SellerVO sellerVO=(SellerVO)session.getAttribute("svo");
		List<ProductVO> list=sellerService.getProductList(sellerVO);
		return new ModelAndView("getProductList.seller","list",list);
	}
	@RequestMapping("/updateProductView.do")
	public ModelAndView updateProductView(ProductVO vo,HttpSession session){
		System.out.println(vo.getProductId());
		ProductVO pvo=sellerService.updateProductView(vo.getProductId());
		List<String> list=sellerService.registerProductView();
		HashMap map=new HashMap();
		map.put("pvo", pvo);
		map.put("list", list);
		return new ModelAndView("updateProductView.seller","map",map);
	}
	@RequestMapping("/updateProduct.do")
	public ModelAndView updateProduct(ProductVO pvo,HttpSession session){
		sellerService.updateProduct(pvo);
		return new ModelAndView("updateProduct.seller");
	}
	@RequestMapping("/deleteProduct.do")
	@ResponseBody
	public List<ProductVO> delelteProduct(String[] productId,HttpSession session){
		for(int i=0;i<productId.length;i++){
			sellerService.delelteProduct(productId[i]);
		}
		SellerVO sellerVO=(SellerVO)session.getAttribute("svo");
		List<ProductVO> list=sellerService.getProductList(sellerVO);
		return list;
	}
	@RequestMapping("/orderByPrice.do")
	@ResponseBody
	public List<ProductVO> getProductListOrderByPrice(HttpSession session){
		SellerVO sellerVO=(SellerVO)session.getAttribute("svo");
		List<ProductVO> list=sellerService.getProductListOrderByPrice(sellerVO);
		return list;
	}
	
	@RequestMapping("/orderByDate.do")
	@ResponseBody
	public List<ProductVO> getProductListOrderByDate(HttpSession session){
		SellerVO sellerVO=(SellerVO)session.getAttribute("svo");
		List<ProductVO> list=sellerService.getProductListOrderByDate(sellerVO);
		return list;
	}
	
	@RequestMapping("/orderBySalesVolume.do")
	@ResponseBody
	public List<ProductVO> getProductListOrderBySalesVolume(HttpSession session){
		SellerVO sellerVO=(SellerVO)session.getAttribute("svo");
		List<ProductVO> list=sellerService.getProductListOrderBySalesVolume(sellerVO);
		return list;
	}
	
	/** upload path */
	private String path="C:\\java-kosta\\final-project\\final-project-khy\\WebContent\\upload\\";
	/**
	 * 상품등록 메서드
	 */
	@RequestMapping("/product.do")
	public String registerProduct(ProductVO vo,FileVO fvo) throws Exception{
		sellerService.registerProduct(vo);
		System.out.println(vo);
		
		System.out.println("vo"+vo);
		System.out.println("fvo"+fvo);
		System.out.println(fvo.getUploadFile());
		
		/*
		 * 파일 업로드 처리 
		 */
		MultipartFile mfile=fvo.getUploadFile();
		if(mfile.isEmpty()==false){
			// 원본 파일명을 반환 
			String fileName=mfile.getOriginalFilename();
			fvo.setOrgfilename(fileName);
			//상품 아이디로 파일명  변환 
			String newfilename=vo.getProductId()+"_"+fileName;
			fvo.setNewfilename(newfilename);
			// 파일 업로드 
			File copyFile=new File(path+newfilename);
			mfile.transferTo(copyFile);
		}
			
		return "home";
	}
	
	/**
	 * 상품등록전에 품목 view 메서드
	 */
	@RequestMapping("/registerProductView.do")
	public ModelAndView registerProductView() throws Exception{
		System.out.println("registerProductView");
		List<String> list=sellerService.getCategoryView();
		System.out.println(list);
		
		
		return new ModelAndView("registerProductView.seller","list",list);
	}
	/**
	 * 상품품목정보를 상품등록 view 에 전달 메서드
	 */
	@RequestMapping("/register.do")
	public ModelAndView register(String productCategory){
		System.out.println(productCategory);
		
		return new ModelAndView("registerProduct.seller","productCategory",productCategory);
	}
	/**
	 * 판매자 정보수정 정보를 보여주는 view  메서드
	 * @throws Exception 
	 */
	@RequestMapping("/updateSellerview.do")
	public ModelAndView updateSellerview(SellerVO svo) throws Exception{
		SellerVO vo=sellerService.updateSellerView("java");
		return new ModelAndView("updateSeller.seller","vo",vo);
	}
	/**
	 * 판매자 정보를 수정하는 메서드 
	 */
	@RequestMapping("/updateSeller.do")
	public String updateSeller(SellerVO svo)throws Exception{
		sellerService.updateSeller(svo);
		return "home";
	}
}
